package day20171117;

import java.io.*;

/**
 * Created by Administrator on 2017-11-17.
 */
public class Test {

    public static void main(String[] args) {

        // 1 字符流

        // 1.1 写文件
        String path = "E:\\hello.txt";
        BufferedWriter writer = null;
        try {
            writer =
                    new BufferedWriter(new FileWriter(path));

            writer.write("hello.word!!");
            writer.newLine();

            writer.write("101010100101010");
            writer.newLine();

            writer.write("bye!!!!!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BufferedReader reader = null;
        try {
             reader = new BufferedReader(new FileReader(path));

            String line = null;
            while( (line = reader.readLine()) != null){

                System.out.println(line);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        // 2 字节流
        // 写文件
        FileOutputStream fileOutputStream = null;
        try {
             fileOutputStream = new FileOutputStream(path);

            // 10101000 - byte
            // 每一次写的是一个或多个整数
            fileOutputStream.write(65); // 'A'

            byte a;

            byte[] data = new byte[3];
            data[0] = 65;
            data[1] = 66;
            data[2] = 67;

            fileOutputStream.write(data);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        FileInputStream fileInputStream = null;
        try {
             fileInputStream = new FileInputStream(path);

            int data;

            while((data = fileInputStream.read()) != -1){


                System.out.println((char) data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

    }
}
